home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #4 / Amiga Plus CD - 2000 - No. 4.iso / Vollversion / CamD / development / examples / vu / vu.c < prev    next >
C/C++ Source or Header  |  2000-05-15  |  7KB  |  240 lines

  1.  
  2. #include "std_headers.h"
  3. #include <exec/interrupts.h>
  4. #include <hardware/intbits.h>
  5. #include <midi/camd.h>
  6. #include <midi/mididefs.h>
  7. #include <clib/camd_protos.h>
  8. #include <pragmas/camd_pragmas.h>
  9.  
  10. #define BYNOTE          1
  11. #define VELSCALE        150
  12.  
  13. struct Library          *IntuitionBase,
  14.                         *GfxBase,
  15.                         *CamdBase;
  16. extern struct Library   *SysBase;
  17.  
  18. struct Window           *Window;
  19. struct Screen           *Screen;
  20. struct ViewPort         *vp;
  21. struct RastPort         *rp;
  22.  
  23. USHORT colors[32] = {
  24.     0x000,0xAAA,0x888,0x555,0x333,0xBB0,0x6B0,0x0F0,
  25.     0xF99,0xF89,0xE8A,0xE8A,0xE7A,0xE7B,0xD6B,0xD6C,
  26.     0xD5D,0xC5D,0xA5C,0x94C,0x84C,0x74B,0x53B,0x43B,
  27.     0x333,0x444,0x555,0x666,0x777,0x888,0xEEE,0xFFF };
  28.  
  29. struct NewScreen NewScreen = { 0,200-47,320,47, 5, 0,1, NULL, CUSTOMSCREEN, };
  30.  
  31. extern struct Image     im1,
  32.                         im2,
  33.                         im3;
  34.  
  35. struct Gadget off_gadget = {
  36.     NULL,   2,2,13,21,GFLG_GADGHIMAGE|GFLG_GADGIMAGE,GACT_RELVERIFY,GTYP_BOOLGADGET,(APTR)&im1,(APTR)&im2
  37. };
  38.  
  39. struct NewWindow back_ground =
  40. {   0,0,320,47, 0,1,
  41.     VANILLAKEY | MOUSEBUTTONS | GADGETUP,
  42.     NOCAREREFRESH | ACTIVATE | BORDERLESS | RMBTRAP,
  43.     &off_gadget,NULL,NULL,NULL,NULL,320,200,320,200,CUSTOMSCREEN,
  44. };
  45.  
  46. #if BYNOTE
  47. #define VU_BARCOUNT     32
  48. #define BARS_PER_COLOR  2
  49. #else
  50. #define VU_BARCOUNT     16
  51. #define BARS_PER_COLOR  1
  52. #endif
  53.  
  54. WORD                    vu_levels[VU_BARCOUNT],
  55.                         prev_levels[VU_BARCOUNT];
  56.  
  57. struct Task             *myTask;
  58.  
  59. void draw_vu_levels(void);
  60.  
  61. int __interrupt __saveds VBlankInterface( void );
  62.  
  63. struct Interrupt VertBlank =
  64. {   NULL,NULL,NT_INTERRUPT,-60,"VU VBlank",     /* node, pri = -60 */
  65.     NULL,                                       /* data ptr, same as inputevent */
  66.     (void *)VBlankInterface                     /* code ptr */
  67. };
  68.  
  69. WORD                    vblank_ok=NULL;         /* TRUE if vblank int installed */
  70.  
  71. struct MidiNode         *midi;
  72. struct MidiLink         *link;
  73.  
  74. struct MidiNode *CreateMidi(Tag tag, ...)
  75. {   return CreateMidiA((struct TagItem *)&tag );
  76. }
  77.  
  78. BOOL SetMidiAttrs(struct MidiNode *mi, Tag tag, ...)
  79. {   return SetMidiAttrsA(mi, (struct TagItem *)&tag );
  80. }
  81.  
  82. struct MidiLink *AddMidiLink(struct MidiNode *mi, LONG type, Tag tag, ...)
  83. {   return AddMidiLinkA(mi, type, (struct TagItem *)&tag );
  84. }
  85.  
  86. BOOL SetMidiLinkAttrs(struct MidiLink *mi, Tag tag, ...)
  87. {   return SetMidiLinkAttrsA(mi, (struct TagItem *)&tag );
  88. }
  89.  
  90. void main(int argc,char *argv[])
  91. {   char    *inlinkname = "in.0";
  92.  
  93.     if (argc > 1) inlinkname = argv[1];
  94.  
  95.     myTask = FindTask(0);
  96.  
  97.     unless (IntuitionBase = OpenLibrary("intuition.library",0)) LEAVE;
  98.     unless (GfxBase = OpenLibrary("graphics.library",0)) LEAVE;
  99.     unless (CamdBase = OpenLibrary("camd.library",0)) LEAVE;
  100.  
  101.     if ((Screen = OpenScreen(&NewScreen)) == NULL) LEAVE;
  102.     vp = &(Screen->ViewPort);
  103.     LoadRGB4(vp,colors,32);
  104.  
  105.     back_ground.Screen = Screen;
  106.     if ((Window = OpenWindow(&back_ground)) == NULL) LEAVE;
  107.     rp = Window->RPort;
  108.  
  109.     DrawImage(rp,&im3,309,0);
  110.     SetAPen(rp, 1); Move(rp,19, 0); Draw(rp,308, 0);
  111.     SetAPen(rp,24); Move(rp,19, 1); Draw(rp,308, 1);
  112.     SetAPen(rp, 3); Move(rp,19, 2); Draw(rp,308, 2);
  113.  
  114.     SetAPen(rp, 1); Move(rp,19,44); Draw(rp,308,44);
  115.     SetAPen(rp,24); Move(rp,19,45); Draw(rp,308,45);
  116.     SetAPen(rp, 3); Move(rp,19,46); Draw(rp,308,46);
  117.  
  118.     unless (midi = CreateMidi(
  119.         MIDI_Name, "VU Meters",
  120.         MIDI_RecvSignal, SIGBREAKB_CTRL_E,
  121.         MIDI_MsgQueue,   100,
  122.         MIDI_ErrFilter, CMEF_All,
  123.         TAG_DONE))
  124.             LEAVE;
  125.  
  126.     unless (link = AddMidiLink(midi, MLTYPE_Receiver,
  127.         MLINK_Name, "VU Meter Link",
  128.         MLINK_Location, inlinkname,
  129.         MLINK_EventMask, CMF_Note,
  130.         MLINK_Comment,  "VU Meters [Input]",
  131.         TAG_DONE))
  132.             LEAVE;
  133.  
  134.     AddIntServer(INTB_VERTB, &VertBlank);
  135.     vblank_ok = TRUE;
  136.  
  137.     while (TRUE)
  138.     {   struct IntuiMessage *message;
  139.         LONG            signals;
  140.  
  141.         signals = Wait((1 << Window->UserPort->mp_SigBit) | SIGBREAKF_CTRL_F | SIGBREAKF_CTRL_E);
  142.  
  143.         if (signals & SIGBREAKF_CTRL_F)
  144.         {   int i;
  145.  
  146.             for (i=0; i<VU_BARCOUNT; i++)
  147.             {   if (vu_levels[i] > 0)
  148.                     vu_levels[i]--;
  149.             }
  150.             draw_vu_levels();
  151.         }
  152.  
  153.         if (signals & SIGBREAKF_CTRL_E)
  154.         {
  155.             MidiMsg     msg;
  156.  
  157.             while (GetMidi(midi,&msg))
  158.             {
  159.                 if (noteon(&msg))
  160. #if BYNOTE
  161.                 {   int     note = ((msg.mm_Data1-36)/2) % 31;
  162.                     int     velocity = msg.mm_Data2 * 39 / 127;
  163.  
  164.                     velocity = velocity * VELSCALE / 100;
  165.                     if (velocity > 39) velocity = 39;
  166.  
  167.                     if (vu_levels[note] < velocity) vu_levels[note] = velocity;
  168.                 }
  169. #else
  170.                 {   int     channel = msg.mm_Status & 0x0f;
  171.                     int     velocity = msg.mm_Data2 * 39 / 127;
  172.  
  173.                     velocity = velocity * VELSCALE / 100;
  174.                     if (velocity > 39) velocity = 39;
  175.  
  176.                     if (vu_levels[channel] < velocity) vu_levels[channel] = velocity;
  177.                 }
  178. #endif
  179.             }
  180.         }
  181.  
  182.         while (message = (struct IntuiMessage *)GetMsg(Window->UserPort))
  183.         {   ULONG       class = message->Class;
  184.  
  185.             ReplyMsg(&message->ExecMessage);
  186.             if (class == GADGETUP) LEAVE;
  187.         }
  188.     }
  189. exitit:
  190.     if (vblank_ok) RemIntServer(INTB_VERTB, &VertBlank);
  191.     if (midi) DeleteMidi(midi);
  192.     if (Window){ ClearMenuStrip(Window); CloseWindow(Window); }
  193.     if (Screen) CloseScreen(Screen);
  194.     if (CamdBase) CloseLibrary(CamdBase);
  195.     if (GfxBase) CloseLibrary(GfxBase);
  196.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  197. }
  198.  
  199. void draw_vu_levels(void)
  200. {   int                 i;
  201.  
  202.     int                 ymin, ymax, y1, y2;
  203.     int                 xmin, xmax;
  204.  
  205.     for (i=0; i<VU_BARCOUNT; i++)
  206.     {
  207.         if (vu_levels[i] > prev_levels[i])
  208.         {
  209.             SetAPen(rp,i/BARS_PER_COLOR+8);
  210.  
  211.             ymin = prev_levels[i];
  212.             ymax = vu_levels[i];
  213.         }
  214.         else
  215.         {   SetAPen(rp,0);
  216.             ymax = prev_levels[i];
  217.             ymin = vu_levels[i];
  218.         }
  219. #if BYNOTE
  220.         xmin = 21 + i * 9;
  221.         xmax = xmin + 7;
  222. #else
  223.         xmin = 21 + i * 18;
  224.         xmax = xmin + 15;
  225. #endif
  226.  
  227.         y1 = 43 - ymax;
  228.         y2 = 43 - ymin - 1;
  229.  
  230.         if (y1 <= y2) RectFill(rp,xmin,y1,xmax,y2);
  231.  
  232.         prev_levels[i] = vu_levels[i];
  233.     }
  234. }
  235.  
  236. int __interrupt __saveds VBlankInterface( void )
  237. {   Signal(myTask,SIGBREAKF_CTRL_F);
  238.     return 0;                                   /* server chain continues       */
  239. }
  240.